package fr.utbm.VI51.lemmings_world_editor;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;


/**
 * @author Adrien
 * 
 * This panel is a 2d representation of the map of a level
 *
 */
public class MapPanel extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The width in pixels
	 */
	private int width;
	
	/**
	 * The height in pixels
	 */
	private int height;
	
	/**
	 * The number of pixels per cases
	 */
	private int pixelsPerCases;
	
	/**
	 * The map associated
	 */
	private Map map;
	
	/**
	 * A good geometric functions provider
	 */
	private Graphics2D g2d;
	
	public MapPanel(int height, int width, int pixelsPerCases)
	{
		this.height = height * pixelsPerCases;
		this.width = width * pixelsPerCases;
		this.pixelsPerCases = pixelsPerCases;
		this.setMap(new Map(height, width));
	}
	
	/**
	 * Change the map configuration and clear the elements
	 * 
	 * @param _height the new height
	 * @param width the new width
	 * @param pixelsPerCases the new number of pixels per case
	 */
	public void changeMapConfig(int _height, int _width, int _pixelsPerCases){
		this.height			= _height * _pixelsPerCases;
		this.width 			= _width * _pixelsPerCases;
		this.pixelsPerCases = _pixelsPerCases;
		
		this.map.setMap(_height,_width);
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paint(java.awt.Graphics)
	 */
	@Override
	public void paint(Graphics g){
		this.g2d = (Graphics2D) g;
		super.paint(this.g2d);
		drawElements();
	}
	
	/**
	 * Draw the elements stored in the map on the screen
	 */
	public void drawElements(){
		for (int i = 0; i < this.height/this.pixelsPerCases; i++) {
			for (int j = 0; j < this.width/this.pixelsPerCases; j++) {
				this.g2d.setColor(this.map.getElementColor(this.map.getMap()[i][j]));
				this.g2d.fillRect(getXInPixel(j), getYInPixel(i), this.pixelsPerCases, this.pixelsPerCases);
			}
			
		}
	}
	
	/**
	 * Returns the position of a given x in cells in pixels
	 * 
	 * @param xInCells the x position in cells
	 * @return the position in pixels
	 */
	public int getXInPixel(int xInCells){
		return xInCells * this.pixelsPerCases;
	}
	
	/**
	 * Returns the position of a given y in cells in pixels
	 * 
	 * @param yInCells the y position in case
	 * @return the position in pixels
	 */
	public int getYInPixel(int yInCells){
		return ((this.height/this.pixelsPerCases) - 1 - yInCells) * this.pixelsPerCases;
	}
	
	/**
	 * Returns the x position in cases from a position in pixels
	 * 
	 * @param xInPixels the x position in pixels
	 * @return the x position in cases
	 */
	public int getXInCases(int xInPixels){
		return (int) Math.floor(xInPixels / this.pixelsPerCases);
	}
	
	/**
	 * Returns the y position in cases from a position in pixels
	 * 
	 * @param yInPixels the y position in pixels
	 * @return the y position in cases
	 */
	public int getYInCases(int yInPixels){
		return (int)Math.floor(((this.height - yInPixels)/this.pixelsPerCases));
	}
	
	/**
	 * Returns the number of pixels per case
	 * 
	 * @return the number of pixels per case
	 */
	public int getPixelsPerCases() {
		return this.pixelsPerCases;
	}

	/**
	 * Set the number of pixels per case
	 * 
	 * @param pixelsPerCases the number of pixels per case
	 */
	public void setPixelsPerCases(int pixelsPerCases) {
		this.pixelsPerCases = pixelsPerCases;
	}

	/**
	 * @param map the map to set
	 */
	public void setMap(Map map) {
		this.map = map;
	}

	/**
	 * @return the map
	 */
	public Map getMap() {
		return this.map;
	}	
	
}
